package com.hqyj.system.controller;

import com.hqyj.system.model.Permission;
import com.hqyj.system.model.Role;
import com.hqyj.system.model.RolePermission;
import frame.system.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cz
 * 2021/9/2 11:56
 */
@Controller
@RequestMapping("/permissionController")
public class PermissionController extends BaseController {

    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        List<Permission> permission_dbList = permissionService.queryAllPermission();
        System.out.println(permission_dbList);
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/addUI.do")
    public String addUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        List<Permission> permission_dbList = permissionService.queryAllPermission();
        System.out.println(permission_dbList);
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/addPermission";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR) {
        //设置父节点
        permission.setpId(Integer.parseInt(permissionIdSTR));
        int n = permissionService.addPermissionByPermission(permission);

        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdStr) {
        int permissionId = Integer.parseInt(permissionIdStr);
        int n = permissionService.deletePermissionByPermissionId(permissionId);

        List<Permission> permission_dbList = permissionService.queryAllPermission();
        System.out.println(permission_dbList);
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String permissionIdStr ) {
        int permissionId = Integer.parseInt(permissionIdStr);
        Permission permission_db = permissionService.queryPermissionByPermissionId(permissionId);
        model.addAttribute("permission_db", permission_db);

        //父级节点数据
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        System.out.println(permission_dbList);
        model.addAttribute("permission_dbList", permission_dbList);

        //2.跳转页面;
        return "view/system/permission/updatePermission";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdStr, String permissionIdSTR) {
        //设置权限ID
        permission.setPermissionId(Integer.parseInt(permissionIdStr));
        //设置权限父ID
        permission.setpId(Integer.parseInt(permissionIdSTR));
        int n = permissionService.updatePermissionByPermission(permission);

        List<Permission> permission_dbList = permissionService.queryAllPermission();
        System.out.println(permission_dbList);
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/FPPermissionUI.do")
    public String FPPermissionUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String roleIdStr) {

        int roleId = Integer.parseInt(roleIdStr);

        //角色数据
        Role role_db = roleService.queryRoleByRoleId(roleId);
        model.addAttribute("role_db", role_db);

        //权限数据
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);

        //选中的数据
        List<Permission> permission_db_XZList = permissionService.queryPermissionByRoleId(roleId);
        List<Integer> list = new ArrayList<>();
        for (Permission permission_db_XZ:permission_db_XZList){
            list.add(permission_db_XZ.getPermissionId());
        }
        request.setAttribute("ids", list);

        //2.跳转页面;
        return "view/system/permission/FPPermissionUI";
    }

    @RequestMapping("/FPPermission.do")
    public String FPPermission(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, RolePermission rolePermission, Permission permission, String roleIdStr, String[] permissionIdSTRs) {

        int roleId = Integer.parseInt(roleIdStr);

        //先删除：判断数据有没有已选权限
        int m = rolePermissionService.deleteRolePermissionByRoleId(roleId);

        for (String permissionIdS : permissionIdSTRs) {
            int permissionId = Integer.parseInt(permissionIdS);
//            System.out.println("-------------2----------" + permissionId);
            //3.调用业务;
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            int n = rolePermissionService.addRolePermissionByRolePermission(rolePermission);
        }

        //2.跳转页面;
        //a.分配角色数据
        Role role_db = roleService.queryRoleByRoleId(roleId);
        model.addAttribute("role_db", role_db);

        //b.分配角色数据
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);

        //c.有情提示
        model.addAttribute("message", "分配权限成功，继续分配吗？");

        //d.选中数据
        List<Permission> permission_db_XZList = permissionService.queryPermissionByRoleId(roleId);
        //返回数据（两个集合对比）
        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission_db_XZ : permission_db_XZList) {
            list.add(permission_db_XZ.getPermissionId());
        }
        request.setAttribute("ids", list);

        return "view/system/permission/FPPermissionUI";
    }
}
